﻿using System;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.Text.RegularExpressions;

namespace Zwms.Client.InfoSys
{
    public partial class SysConfigWnd : UserControl
    {
        #region 成员变量
        private SysConfig _sysConfig = null;
        #endregion

        #region 构造函数
        public SysConfigWnd()
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnRefresh.Image = global::Zwms.Client.Properties.Resources.refresh;
            this.btnUpdate.Image = global::Zwms.Client.Properties.Resources.ok;
        }

        private void InitControl()
        {
            //ServiceReferenceZwms.ServiceZwmsClient client
            //    = new ServiceReferenceZwms.ServiceZwmsClient();

            //_sysConfig = client.GetSysConfig();
            //if (_sysConfig == null)
            //{
            //    return;
            //}
            //else
            //{
            //    tbPrinterCfg.Text = _sysConfig.PrinterCfg;
            //}
        }
        #endregion

        #region 控件函数
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            InitControl();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //_sysConfig.PrinterCfg = tbPrinterCfg.Text;
            //ServiceReferenceZwms.ServiceZwmsClient client
            //    = new ServiceReferenceZwms.ServiceZwmsClient();
            //if (client.UpdateSysConfig(_sysConfig))
            //{
            //    MessageBox.Show("成功！");
            //}
            //else
            //{
            //    return;
            //}
            //Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"];
            ServiceModelSectionGroup serviceModelSectionGroup = sct as ServiceModelSectionGroup;
            ClientSection clientSection = serviceModelSectionGroup.Client;

            foreach (ChannelEndpointElement item in clientSection.Endpoints)
            {
                string pattern = "://.*/";
                string address = item.Address.ToString();
                if (tbPort.Text.Trim() != "")
                {
                    string replacement = string.Format("://{0}:{1}/", tbServerAddress.Text, tbPort.Text);
                    address = Regex.Replace(address, pattern, replacement);
                    item.Address = new Uri(address);
                }
                else
                {
                    string replacement = string.Format("://{0}/", tbServerAddress.Text);
                    address = Regex.Replace(address, pattern, replacement);
                    item.Address = new Uri(address);
                }
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("system.serviceModel");

        }
        #endregion
    }
}
